using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Data;
using Cleverscape.UTorrentClient.WebClient;

namespace uTorrentNetClient.ModelDesign.Converter
{
    [ValueConversion(typeof(TimeSpan), typeof(double))]
    public class TimeSpanHoursConverter : IValueConverter
    {
        public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
        {
            if (targetType != typeof(string))
            {
                return 0;
            }
            if (value is TimeSpan)
            {
                var t = (TimeSpan) value;
                return t.TotalHours;
            }
            return 0;
        }

        public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
        {
            if (targetType != typeof(TimeSpan))
            {
                return TimeSpan.FromHours(0); ;
            }
            if (value is string)
            {
                double t = 0;
                double.TryParse(value.ToString(), out t);
                return TimeSpan.FromHours(t);
            }
            return TimeSpan.FromHours(0);
        }
    }
}
